<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="ios,xcode,swift," />





  <link rel="alternate" href="/atom.xml" title="搁浅丶岁月的变迁" type="application/atom+xml" />






<meta name="description" content="Xcode 8.3 和 Swift 3.1 现在已经发布了(3/28)！ 可以通过 AppStore 或 Apple Developer 进行下载  Xcode 8.3 优化了 Objective-C 与 Swift 混编项目的编译速度. Swift 3.1 版本包含一些期待已久的 Swift package manager 功能和语法本身的改进。 如果您没有密切关注 Swift Evoluti">
<meta name="keywords" content="ios,xcode,swift">
<meta property="og:type" content="article">
<meta property="og:title" content="Swift 3.1 的新变化「译」">
<meta property="og:url" content="http://yoursite.com/2017/03/30/2017-03-30-Swift 3.1-的新变化「译」/index.html">
<meta property="og:site_name" content="搁浅丶岁月的变迁">
<meta property="og:description" content="Xcode 8.3 和 Swift 3.1 现在已经发布了(3/28)！ 可以通过 AppStore 或 Apple Developer 进行下载  Xcode 8.3 优化了 Objective-C 与 Swift 混编项目的编译速度. Swift 3.1 版本包含一些期待已久的 Swift package manager 功能和语法本身的改进。 如果您没有密切关注 Swift Evoluti">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://koenig-media.raywenderlich.com/uploads/2017/03/WhatsNewSwift3.1-feature-1-250x250.png">
<meta property="og:image" content="https://ww2.sinaimg.cn/large/006tNbRwgy1fe2it3xwt6j30s50g3mya.jpg">
<meta property="og:updated_time" content="2018-03-08T07:33:10.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Swift 3.1 的新变化「译」">
<meta name="twitter:description" content="Xcode 8.3 和 Swift 3.1 现在已经发布了(3/28)！ 可以通过 AppStore 或 Apple Developer 进行下载  Xcode 8.3 优化了 Objective-C 与 Swift 混编项目的编译速度. Swift 3.1 版本包含一些期待已久的 Swift package manager 功能和语法本身的改进。 如果您没有密切关注 Swift Evoluti">
<meta name="twitter:image" content="https://koenig-media.raywenderlich.com/uploads/2017/03/WhatsNewSwift3.1-feature-1-250x250.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/03/30/2017-03-30-Swift 3.1-的新变化「译」/"/>





  <title>Swift 3.1 的新变化「译」 | 搁浅丶岁月的变迁</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
    <a href="https://github.com/liaoren512"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_left_red_aa0000.png" alt="Fork me on GitHub"></a>
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">搁浅丶岁月的变迁</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">没有虽败犹荣，只有成王败寇。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/03/30/2017-03-30-Swift 3.1-的新变化「译」/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="廖壬">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/ichgo.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="搁浅丶岁月的变迁">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Swift 3.1 的新变化「译」</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-03-30T00:00:00+08:00">
                2017-03-30
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://koenig-media.raywenderlich.com/uploads/2017/03/WhatsNewSwift3.1-feature-1-250x250.png" alt=""></p>
<p>Xcode 8.3 和 Swift 3.1 现在已经发布了(3/28)！</p>
<p>可以通过 <a href="https://itunes.apple.com/us/app/xcode/id497799835?ls=1&amp;mt=12#" target="_blank" rel="noopener">AppStore</a> 或 <a href="https://developer.apple.com/download/more/" target="_blank" rel="noopener">Apple Developer</a> 进行下载</p>
<p><img src="https://ww2.sinaimg.cn/large/006tNbRwgy1fe2it3xwt6j30s50g3mya.jpg" alt=""></p>
<p>Xcode 8.3 优化了 Objective-C 与 Swift 混编项目的编译速度.</p>
<p>Swift 3.1 版本包含一些期待已久的 <a href="https://github.com/apple/swift-package-manager" target="_blank" rel="noopener">Swift package manager</a> 功能和语法本身的改进。</p>
<p>如果您没有密切关注 <a href="https://github.com/apple/swift-evolution" target="_blank" rel="noopener">Swift Evolution</a> 进程，请继续阅读 - 本文非常适合您！</p>
<p>在本文中，我将强调Swift 3.1中最重要的变化，这将对您的代码产生重大影响。我们来吧！😃</p>
<h2 id="开始"><a href="#开始" class="headerlink" title="开始"></a>开始</h2><p>Swift 3.1与Swift 3.0源代码兼容，因此如果您已经使用Xcode 中的 <code>Edit \ Convert \ To Current Swift Syntax ...</code> 将项目迁移到Swift 3.0，新功能将不会破坏您的代码。不过，苹果已经在Xcode 8.3中支持Swift 2.3。所以如果你还没有从Swift 2.3迁移，现在是时候这样做了！</p>
<p>在下面的部分，您会看到链接的标签，如<code>[SE-0001]</code>。这些是 <a href="https://apple.github.io/swift-evolution/" target="_blank" rel="noopener">Swift Evolution</a> 提案号码。我已经列出了每个提案的链接，以便您可以发现每个特定更改的完整详细信息。我建议您尝试在Playground上验证新的功能，以便更好地了解所有更改的内容。</p>
<blockquote>
<p>Note:如果你想了解 swift 3.0 中的新功能，可以看<a href="https://www.raywenderlich.com/135655/whats-new-swift-3" target="_blank" rel="noopener">这篇文章</a>。</p>
</blockquote>
<h2 id="语法改进"><a href="#语法改进" class="headerlink" title="语法改进"></a>语法改进</h2><p>首先，我们来看看这个版本中的语法改进，包括关于数值类型的<code>可失败构造器</code>（<code>Failable Initializers</code>），新的序列函数等等。</p>
<h4 id="可失败的数值转换构造器（Failable-Numeric-Conversion-Initializers）"><a href="#可失败的数值转换构造器（Failable-Numeric-Conversion-Initializers）" class="headerlink" title="可失败的数值转换构造器（Failable Numeric Conversion Initializers）"></a>可失败的数值转换构造器（Failable Numeric Conversion Initializers）</h4><p>Swift 3.1 为所有数值类型 <code>(Int, Int8, Int16, Int32, Int64, UInt, UInt8, UInt16, UInt32, UInt64, Float, Float80, Double)</code> 添加了<a href="https://www.swiftmi.com/topic/121.html" target="_blank" rel="noopener">可失败构造器</a>。</p>
<p>这个功能非常有用，例如，以安全、可恢复的方式处理外源松散类型数据的转换，下面来看 Student 的 JSON 数组的处理：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> name: <span class="type">String</span></span><br><span class="line">  <span class="keyword">let</span> grade: <span class="type">Int</span></span><br><span class="line"> </span><br><span class="line">  <span class="keyword">init</span>?(json: [<span class="type">String</span>: <span class="type">Any</span>]) &#123;</span><br><span class="line">    <span class="keyword">guard</span> <span class="keyword">let</span> name = json[<span class="string">"name"</span>] <span class="keyword">as</span>? <span class="type">String</span>,</span><br><span class="line">          <span class="keyword">let</span> gradeString = json[<span class="string">"grade"</span>] <span class="keyword">as</span>? <span class="type">String</span>,</span><br><span class="line">          <span class="keyword">let</span> gradeDouble = <span class="type">Double</span>(gradeString),</span><br><span class="line">          <span class="keyword">let</span> grade = <span class="type">Int</span>(exactly: gradeDouble)  <span class="comment">// &lt;-- 3.1 的改动在这</span></span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">nil</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">self</span>.name = name</span><br><span class="line">    <span class="keyword">self</span>.grade = grade</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">makeStudents</span><span class="params">(with data: Data)</span></span> -&gt; [<span class="type">Student</span>] &#123;</span><br><span class="line">  <span class="keyword">guard</span> <span class="keyword">let</span> json = <span class="keyword">try</span>? <span class="type">JSONSerialization</span>.jsonObject(with: data, options: .allowFragments),</span><br><span class="line">        <span class="keyword">let</span> jsonArray = json <span class="keyword">as</span>? [[<span class="type">String</span>: <span class="type">Any</span>]] <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> []</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> jsonArray.flatMap(<span class="type">Student</span>.<span class="keyword">init</span>)</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">let</span> rawStudents = <span class="string">"[&#123;\"name\":\"Ray\", \"grade\":\"5.0\"&#125;, &#123;\"name\":\"Matt\", \"grade\":\"6\"&#125;,</span></span><br><span class="line"><span class="string">                    &#123;\"name\":\"Chris\", \"grade\":\"6.33\"&#125;, &#123;\"name\":\"Cosmin\", \"grade\":\"7\"&#125;, </span></span><br><span class="line"><span class="string">                    &#123;\"name\":\"Steven\", \"grade\":\"7.5\"&#125;]"</span></span><br><span class="line"><span class="keyword">let</span> data = rawStudents.data(using: .utf8)!</span><br><span class="line"><span class="keyword">let</span> students = makeStudents(with: data)</span><br><span class="line"><span class="built_in">dump</span>(students) <span class="comment">// [(name: "Ray", grade: 5), (name: "Matt", grade: 6), (name: "Cosmin", grade: 7)]</span></span><br></pre></td></tr></table></figure>
<p>在 <code>Student</code> 类中使用了一个可失败构造器将 <code>grade</code> 属性从 <code>Double</code> 转变为 <code>Int</code>，像这样</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> grade = <span class="type">Int</span>(exactly: gradeDouble)</span><br></pre></td></tr></table></figure>
<p>如果<code>gradeDouble</code>不是整数，例如6.33，它将失败。如果它可以用一个正确的表示Int，例如6.0，它将成功。</p>
<blockquote>
<p>Note：虽然<code>throwing initializers</code> 可以用来替代 <code>failable initializers</code>。但是使用 <code>failable initializers</code> 会更好，更符合人的思维。</p>
</blockquote>
<h5 id="新的序列函数（Sequence-Functions）"><a href="#新的序列函数（Sequence-Functions）" class="headerlink" title="新的序列函数（Sequence Functions）"></a>新的序列函数（Sequence Functions）</h5><p>swift3.1添加了两个新的标准库函数在 <code>Sequence</code> 协议中：<code>prefix(while:)`</code>和prefix(while:)`<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0045-scan-takewhile-dropwhile.md" target="_blank" rel="noopener">[SE-0045]</a>。</p>
<p>构造一个<a href="https://en.wikipedia.org/wiki/Fibonacci_number" target="_blank" rel="noopener">斐波纳契</a>无限序列：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> fibonacci = sequence(state: (<span class="number">0</span>, <span class="number">1</span>)) &#123;</span><br><span class="line">  (state: <span class="keyword">inout</span> (<span class="type">Int</span>, <span class="type">Int</span>)) -&gt; <span class="type">Int</span>? <span class="keyword">in</span></span><br><span class="line">  <span class="keyword">defer</span> &#123;state = (state.<span class="number">1</span>, state.<span class="number">0</span> + state.<span class="number">1</span>)&#125;</span><br><span class="line">  <span class="keyword">return</span> state.<span class="number">0</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在Swift 3.0中，您只需指定<code>迭代次数</code>即可遍历fibonacci序列：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Swift 3.0</span></span><br><span class="line"><span class="keyword">for</span> number <span class="keyword">in</span> fibonacci.<span class="keyword">prefix</span>(<span class="number">10</span>) &#123;</span><br><span class="line">  <span class="built_in">print</span>(number)  <span class="comment">// 0 1 1 2 3 5 8 13 21 34</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在swift 3.1中，您可以使用<code>prefix(while:)</code>和<code>drop(while:)</code>获得符合条件在两个给定值之间的序列中的所有元素，就像这样：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Swift 3.1</span></span><br><span class="line"><span class="keyword">let</span> interval = fibonacci.<span class="keyword">prefix</span>(<span class="keyword">while</span>: &#123;$<span class="number">0</span> &lt; <span class="number">1000</span>&#125;).drop(<span class="keyword">while</span>: &#123;$<span class="number">0</span> &lt; <span class="number">100</span>&#125;)</span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> interval &#123;</span><br><span class="line">  <span class="built_in">print</span>(element) <span class="comment">// 144 233 377 610 987</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>prefix(while:)</code>返回满足某个谓词的最长子序列。它从序列的开头开始，并停在给定闭包返回false的第一个元素上。</p>
<p><code>drop(while:)</code> 相反：它返回从给定关闭返回false的第一个元素开始的子序列，并在序列结尾完成。</p>
<blockquote>
<p>Note:这种情况，可以使用尾随闭包的写法：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&gt; <span class="keyword">let</span> interval = fibonacci.<span class="keyword">prefix</span>&#123;$<span class="number">0</span> &lt; <span class="number">1000</span>&#125;.drop&#123;$<span class="number">0</span> &lt; <span class="number">100</span>&#125;</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="Concrete-Constrained-Extensions（姑且翻译为类的约束扩展吧）"><a href="#Concrete-Constrained-Extensions（姑且翻译为类的约束扩展吧）" class="headerlink" title="Concrete Constrained Extensions（姑且翻译为类的约束扩展吧）"></a>Concrete Constrained Extensions（姑且翻译为类的约束扩展吧）</h2><p>Swift 3.1允许您扩展具有类型约束的通用类型。以前，你不能像这样扩展类型，因为约束必须是一个协议。我们来看一个例子。</p>
<p>例如，Ruby on Rails提供了一种<code>isBlank</code>检查用户输入的非常有用的方法。以下是在Swift 3.0中用 <code>String</code> 类型的扩展实现这个<strong>计算型属性</strong>：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Swift 3.0</span></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">String</span> </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> isBlank: <span class="type">Bool</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> trimmingCharacters(<span class="keyword">in</span>: .whitespaces).isEmpty</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">let</span> abc = <span class="string">" "</span></span><br><span class="line"><span class="keyword">let</span> def = <span class="string">"x"</span></span><br><span class="line"> </span><br><span class="line">abc.isBlank <span class="comment">// true</span></span><br><span class="line">def.isBlank <span class="comment">// false</span></span><br></pre></td></tr></table></figure>
<p>如果你希望<code>isBlank</code>计算型属性为一个可选值所用，在swift 3.0中，你将要这样做</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Swift 3.0</span></span><br><span class="line"><span class="class"><span class="keyword">protocol</span> <span class="title">StringProvider</span> </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> string: <span class="type">String</span> &#123;<span class="keyword">get</span>&#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">String</span>: <span class="title">StringProvider</span> </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> string: <span class="type">String</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">self</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">Optional</span> <span class="title">where</span> <span class="title">Wrapped</span>: <span class="title">StringProvider</span> </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> isBlank: <span class="type">Bool</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">self</span>?.string.isBlank ?? <span class="literal">true</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">let</span> foo: <span class="type">String</span>? = <span class="literal">nil</span></span><br><span class="line"><span class="keyword">let</span> bar: <span class="type">String</span>? = <span class="string">"  "</span></span><br><span class="line"><span class="keyword">let</span> baz: <span class="type">String</span>? = <span class="string">"x"</span></span><br><span class="line"> </span><br><span class="line">foo.isBlank <span class="comment">// true</span></span><br><span class="line">bar.isBlank <span class="comment">// true</span></span><br><span class="line">baz.isBlank <span class="comment">// false</span></span><br></pre></td></tr></table></figure>
<p>这创建了一个采用 <code>String</code> 的 <code>StringProvider</code> 协议而在你使用StringProvider扩展可选的 wrapped 类型时,添加isBlank方法。</p>
<p>Swift 3.1中，用来替代协议方法，扩展具体类型的方法像这样：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Swift 3.1</span></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">Optional</span> <span class="title">where</span> <span class="title">Wrapped</span> == <span class="title">String</span> </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> isBlank: <span class="type">Bool</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">self</span>?.isBlank ?? <span class="literal">true</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这就用更少的代码实现了和原先相同的功能~</p>
<h2 id="泛型嵌套（Nested-Generics）"><a href="#泛型嵌套（Nested-Generics）" class="headerlink" title="泛型嵌套（Nested Generics）"></a>泛型嵌套（Nested Generics）</h2><p>Swift 3.1允许您将嵌套类型与泛型混合。作为一个练习，考虑这个（不是太疯狂）的例子。每当某个团队领导raywenderlich.com想在博客上发布一篇文章时，他会分配一批专门的开发人员来处理这个问题，以满足网站的高质量标准：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Team</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">  <span class="class"><span class="keyword">enum</span> <span class="title">TeamType</span> </span>&#123;</span><br><span class="line">    <span class="keyword">case</span> swift</span><br><span class="line">    <span class="keyword">case</span> iOS</span><br><span class="line">    <span class="keyword">case</span> macOS</span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">BlogPost</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">BlogPostType</span> </span>&#123;</span><br><span class="line">      <span class="keyword">case</span> tutorial</span><br><span class="line">      <span class="keyword">case</span> article</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">let</span> title: <span class="type">T</span></span><br><span class="line">    <span class="keyword">let</span> type: <span class="type">BlogPostType</span></span><br><span class="line">    <span class="keyword">let</span> category: <span class="type">TeamType</span></span><br><span class="line">    <span class="keyword">let</span> publishDate: <span class="type">Date</span></span><br><span class="line"> </span><br><span class="line">    <span class="keyword">init</span>(title: <span class="type">T</span>, type: <span class="type">BlogPostType</span>, category: <span class="type">TeamType</span>, publishDate: <span class="type">Date</span>) &#123;</span><br><span class="line">      <span class="keyword">self</span>.title = title</span><br><span class="line">      <span class="keyword">self</span>.type = type</span><br><span class="line">      <span class="keyword">self</span>.category = category</span><br><span class="line">      <span class="keyword">self</span>.publishDate = publishDate</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">  <span class="keyword">let</span> type: <span class="type">TeamType</span></span><br><span class="line">  <span class="keyword">let</span> author: <span class="type">T</span></span><br><span class="line">  <span class="keyword">let</span> teamLead: <span class="type">T</span></span><br><span class="line">  <span class="keyword">let</span> blogPost: <span class="type">BlogPost</span>&lt;<span class="type">T</span>&gt;</span><br><span class="line"> </span><br><span class="line">  <span class="keyword">init</span>(type: <span class="type">TeamType</span>, author: <span class="type">T</span>, teamLead: <span class="type">T</span>, blogPost: <span class="type">BlogPost</span>&lt;<span class="type">T</span>&gt;) &#123;</span><br><span class="line">    <span class="keyword">self</span>.type = type</span><br><span class="line">    <span class="keyword">self</span>.author = author</span><br><span class="line">    <span class="keyword">self</span>.teamLead = teamLead</span><br><span class="line">    <span class="keyword">self</span>.blogPost = blogPost</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>将<code>BlogPost</code>内部类嵌套在其对应的<code>Team</code>外部类中，并使两个类都通用。这是团队如何寻找我在网站上发布的教程和文章：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Team</span>(type: .swift, author: <span class="string">"Cosmin Pupăză"</span>, teamLead: <span class="string">"Ray Fix"</span>, </span><br><span class="line">     blogPost: <span class="type">Team</span>.<span class="type">BlogPost</span>(title: <span class="string">"Pattern Matching"</span>, type: .tutorial, </span><br><span class="line">     category: .swift, publishDate: <span class="type">Date</span>()))</span><br><span class="line"> </span><br><span class="line"><span class="type">Team</span>(type: .swift, author: <span class="string">"Cosmin Pupăză"</span>, teamLead: <span class="string">"Ray Fix"</span>, </span><br><span class="line">     blogPost: <span class="type">Team</span>.<span class="type">BlogPost</span>(title: <span class="string">"What's New in Swift 3.1?"</span>, type: .article, </span><br><span class="line">     category: .swift, publishDate: <span class="type">Date</span>()))</span><br></pre></td></tr></table></figure>
<p>但实际上，在这种情况下，您可以简化该代码。如果嵌套的内部类型使用通用外部类型，那么它默认继承父类的类型。因此，您不需要如此声明：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Team</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">  <span class="comment">// original code </span></span><br><span class="line"> </span><br><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">BlogPost</span> </span>&#123;</span><br><span class="line">    <span class="comment">// original code</span></span><br><span class="line">  &#125;  </span><br><span class="line"> </span><br><span class="line">  <span class="comment">// original code </span></span><br><span class="line">  <span class="keyword">let</span> blogPost: <span class="type">BlogPost</span></span><br><span class="line"> </span><br><span class="line">  <span class="keyword">init</span>(type: <span class="type">TeamType</span>, author: <span class="type">T</span>, teamLead: <span class="type">T</span>, blogPost: <span class="type">BlogPost</span>) &#123;</span><br><span class="line">    <span class="comment">// original code   </span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>Note：如果您想了解更多关于Swift中的<strong>泛型</strong>，请阅读我们最近更新的<a href="https://www.raywenderlich.com/154371/swift-generics-tutorial-getting-started" target="_blank" rel="noopener">Swift泛型入门的教程</a>。</p>
</blockquote>
<h2 id="Swift版本的可用性"><a href="#Swift版本的可用性" class="headerlink" title="Swift版本的可用性"></a>Swift版本的可用性</h2><p>您可以使用<strong>#if swift(&gt;= N)</strong> <code>静态构造</code>来检查特定的Swift版本：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Swift 3.0</span></span><br><span class="line">#<span class="keyword">if</span> swift(&gt;=<span class="number">3.1</span>)</span><br><span class="line">  <span class="function"><span class="keyword">func</span> <span class="title">intVersion</span><span class="params">(number: Double)</span></span> -&gt; <span class="type">Int</span>? &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="type">Int</span>(exactly: number)</span><br><span class="line">  &#125;</span><br><span class="line">#elseif swift(&gt;=<span class="number">3.0</span>)</span><br><span class="line">  <span class="function"><span class="keyword">func</span> <span class="title">intVersion</span><span class="params">(number: Double)</span></span> -&gt; <span class="type">Int</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="type">Int</span>(number)</span><br><span class="line">  &#125;</span><br><span class="line">#endif</span><br></pre></td></tr></table></figure>
<p>然而，当使用Swift标准库时，这种方法有一个主要缺点。它需要为每个受支持的旧语言版本编译标准库。这是因为当您以向后兼容模式运行Swift编译器时，例如您要使用Swift 3.0行为，则需要使用针对该特定兼容性版本编译的标准库版本。如果您使用版本3.1模式编译的，那么您根本就没有正确的代码</p>
<p>因此，@available除了现有平台版本 <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0141-available-by-swift-version.md" target="_blank" rel="noopener">[SE-0141]</a> 之外，Swift 3.1扩展了该属性以支持指定Swift版本号：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Swift 3.1</span></span><br><span class="line"> </span><br><span class="line"><span class="meta">@available</span>(swift <span class="number">3.1</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">intVersion</span><span class="params">(number: Double)</span></span> -&gt; <span class="type">Int</span>? &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="type">Int</span>(exactly: number)</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="meta">@available</span>(swift, introduced: <span class="number">3.0</span>, obsoleted: <span class="number">3.1</span>)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">intVersion</span><span class="params">(number: Double)</span></span> -&gt; <span class="type">Int</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="type">Int</span>(number)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个新功能提供了与<code>intVersionSwift</code>版本有关的方法相同的行为。但是，它只允许像标准库这样的库被编译一次。编译器然后简单地选择可用于所选择的给定兼容性版本的功能。</p>
<blockquote>
<p>Note：注意：如果您想了解更多关于Swift 的<code>可用性属性（ availability attributes）</code>，请参阅我们关于<a href="https://www.raywenderlich.com/139077/availability-attributes-swift" target="_blank" rel="noopener">Swift中可用性属性的教程</a>。</p>
</blockquote>
<h2 id="逃逸闭包（Escaping-Closures）"><a href="#逃逸闭包（Escaping-Closures）" class="headerlink" title="逃逸闭包（Escaping Closures）"></a>逃逸闭包（Escaping Closures）</h2><p>在Swift 3.0 <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0103-make-noescape-default.md" target="_blank" rel="noopener">[ SE-0103 ]</a> 中函数中的闭包的参数是默认是不逃逸的（non-escaping）。在Swift 3.1中，您可以使用新的函数<code>withoutActuallyEscaping()</code>将非逃逸闭包转换为临时逃逸。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">perform</span><span class="params">(<span class="number">_</span> f: <span class="params">()</span></span></span> -&gt; <span class="type">Void</span>, simultaneouslyWith g: () -&gt; <span class="type">Void</span>,</span><br><span class="line">             on queue: <span class="type">DispatchQueue</span>) &#123;</span><br><span class="line">  withoutActuallyEscaping(f) &#123; escapableF <span class="keyword">in</span>     <span class="comment">// 1</span></span><br><span class="line">    withoutActuallyEscaping(g) &#123; escapableG <span class="keyword">in</span></span><br><span class="line">      queue.async(execute: escapableF)           <span class="comment">// 2</span></span><br><span class="line">      queue.async(execute: escapableG)     </span><br><span class="line"> </span><br><span class="line">      queue.sync(flags: .barrier) &#123;&#125;             <span class="comment">// 3</span></span><br><span class="line">    &#125;                                            <span class="comment">// 4</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此函数同时加载两个闭包，然后在两个完成之后返回。</p>
<ol>
<li><code>f</code> 与 <code>g</code> 进入函数后由非逃逸状态，分别转换为逃逸闭包:<code>escapableF</code>和<code>escapableG</code>。</li>
<li>async(execute:) 的调用需要逃逸闭包，我们在上面已经进行了转换。</li>
<li>通过运行<code>sync(flags: .barrier)</code>，您确保<code>async(execute:)</code>方法完全完成，稍后将不会调用闭包。</li>
<li>在范围内使用 <code>escapableF</code> and <code>escapableG</code>.</li>
</ol>
<p>如果你存储临时逃离闭包(即真正逃脱)这将是一个Bug。未来版本的标准库可以检测这个陷阱,如果你试图调用它们。</p>
<h2 id="Swift-Package-Manager-更新"><a href="#Swift-Package-Manager-更新" class="headerlink" title="Swift Package Manager 更新"></a>Swift Package Manager 更新</h2><p>啊，期待已久的 <strong>Swift Package Manage</strong> 的更新了！</p>
<h4 id="可编辑软件包（Editable-Packages）"><a href="#可编辑软件包（Editable-Packages）" class="headerlink" title="可编辑软件包（Editable Packages）"></a>可编辑软件包（Editable Packages）</h4><p>Swift 3.1将<code>可编辑软件包(editable packages)</code>的概念添加到Swift软件包管理器 <a href="https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#editable-packages" target="_blank" rel="noopener">[ SE-0082 ]</a>。</p>
<p>该<code>swift package edit</code>命令使用现有的<code>Packages</code>并将其转换为<code>editable Packages</code>。使用<code>--end-edit</code>命令将 <code>package manager</code> 还原回 <strong>规范解析的软件包（canonical resolved packag）</strong>。</p>
<h4 id="版本固定（Version-Pinning）"><a href="#版本固定（Version-Pinning）" class="headerlink" title="版本固定（Version Pinning）"></a>版本固定（Version Pinning）</h4><p>Swift 3.1 添加了版本固定的概念[ SE-0145 ]。该 <code>pin</code> 命令 固定一个或所有依赖关系如下所示：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ swift package pin --all      <span class="comment">// 固定所有的依赖</span></span><br><span class="line">$ swift package pin <span class="type">Foo</span>        <span class="comment">// 固定 Foo 在当前的闭包</span></span><br><span class="line">$ swift package pin <span class="type">Foo</span> --version <span class="number">1.2</span>.<span class="number">3</span>  <span class="comment">// 固定 Foo 在 1.2.3 版本</span></span><br></pre></td></tr></table></figure>
<p>使用<code>unpin</code>命令恢复到以前的包版本：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ swift package unpin —all</span><br><span class="line">$ swift package unpin <span class="type">Foo</span></span><br></pre></td></tr></table></figure>
<p>Package manager 将每个依赖库的版本固定信息存储在 <code>Package.pins</code> 文件中。如果该文件不存在，则Package manager 会自动创建。</p>
<h4 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h4><p><code>swift package reset</code> 命令将会把 Package 重置干净。</p>
<p><code>swift test --parallel</code> 命令 执行测试。</p>
<h2 id="其他改动"><a href="#其他改动" class="headerlink" title="其他改动"></a>其他改动</h2><p>在 swift 3.1 中还有一些小改动</p>
<h4 id="多重返回函数"><a href="#多重返回函数" class="headerlink" title="多重返回函数"></a>多重返回函数</h4><p>C函数返回两次，例如<code>vfork</code> 和 <code>vfork</code>已经不用了。他们以有趣的方式改变了程序的控制流程。所以 Swift 社区 已经禁止了该行为，以免导致编译错误。</p>
<h4 id="自动链接失效（Disable-Auto-Linking）"><a href="#自动链接失效（Disable-Auto-Linking）" class="headerlink" title="自动链接失效（Disable Auto-Linking）"></a>自动链接失效（Disable Auto-Linking）</h4><p><a href="https://github.com/apple/swift-package-manager" target="_blank" rel="noopener">Swift Package Manager</a> 禁用了在C语言 <a href="http://nsomar.com/modular-framework-creating-and-using-them/" target="_blank" rel="noopener"><strong>模块映射（module maps）</strong></a>中的<strong>自动链接</strong>的功能：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Swift 3.0</span></span><br><span class="line">module <span class="type">MyCLib</span> &#123;</span><br><span class="line">    header “foo.h<span class="string">"</span></span><br><span class="line"><span class="string">    link “MyCLib"</span></span><br><span class="line">    export *</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="comment">// Swift 3.1</span></span><br><span class="line">module <span class="type">MyCLib</span> &#123;</span><br><span class="line">    header “foo.h”</span><br><span class="line">    export *</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>Swift 3.1改善了Swift 3.0的一些功能，为即将到来的Swift 4.0的大改动做准备。这些包括对泛型，正则表达式，更科学的<code>String</code>等方面的作出极大的改进。</p>
<p>如果你想了解更多，请转到 <a href="https://developer.apple.com/reference/swift?changes=latest_minor" target="_blank" rel="noopener">Swift standard library diffs</a> 或者查看官方的的<a href="https://github.com/apple/swift/blob/master/CHANGELOG.md" target="_blank" rel="noopener">Swift CHANGELOG</a>，您可以在其中阅读所有更改的信息。或者您可以使用它来了解 <strong>Swift 4.0</strong> 中的内容！</p>
<h2 id="著作权声明"><a href="#著作权声明" class="headerlink" title="著作权声明"></a>著作权声明</h2><p>本文译自 <a href="https://www.raywenderlich.com/156352/whats-new-in-swift-3-1" target="_blank" rel="noopener">What’s New in Swift 3.1?</a></p>
<p>由<a href="http://github.com/qiubaiying" target="_blank" rel="noopener">@柏荧(BY)</a>进行翻译,首次发布于 <a href="http://qiubaiying.github.io" target="_blank" rel="noopener">BY Blog</a>，转载请保留原文链接.</p>

      
    </div>
    
    
    

    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <!--<a href="/tags/ios/" rel="tag"># ios</a>-->
            <a href="/tags/ios/" rel="tag"><i class="fa fa-tag"></i> ios</a>
          
            <!--<a href="/tags/xcode/" rel="tag"># xcode</a>-->
            <a href="/tags/xcode/" rel="tag"><i class="fa fa-tag"></i> xcode</a>
          
            <!--<a href="/tags/swift/" rel="tag"># swift</a>-->
            <a href="/tags/swift/" rel="tag"><i class="fa fa-tag"></i> swift</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/03/23/2017-03-23-AsyncDisplayKit-2.0-教程：入门「译」/" rel="next" title="AsyncDisplayKit 2.0 教程:入门「译」">
                <i class="fa fa-chevron-left"></i> AsyncDisplayKit 2.0 教程:入门「译」
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/04/07/2017-04-07-强化-Swift-中的-print/" rel="prev" title="强化 swift 中的 print">
                强化 swift 中的 print <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/ichgo.jpg"
                alt="廖壬" />
            
              <p class="site-author-name" itemprop="name">廖壬</p>
              <p class="site-description motion-element" itemprop="description">Learn to choose, to forsake, to endure loneliness, to resist temptation.</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">391</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">56</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/liaoren512" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:liaoren512@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5281757990/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-globe"></i>Weibo</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com" target="_blank" title="Zhihu">
                      
                        <i class="fa fa-fw fa-globe"></i>Zhihu</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#开始"><span class="nav-number">1.</span> <span class="nav-text">开始</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#语法改进"><span class="nav-number">2.</span> <span class="nav-text">语法改进</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#可失败的数值转换构造器（Failable-Numeric-Conversion-Initializers）"><span class="nav-number">2.0.1.</span> <span class="nav-text">可失败的数值转换构造器（Failable Numeric Conversion Initializers）</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#新的序列函数（Sequence-Functions）"><span class="nav-number">2.0.1.1.</span> <span class="nav-text">新的序列函数（Sequence Functions）</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Concrete-Constrained-Extensions（姑且翻译为类的约束扩展吧）"><span class="nav-number">3.</span> <span class="nav-text">Concrete Constrained Extensions（姑且翻译为类的约束扩展吧）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#泛型嵌套（Nested-Generics）"><span class="nav-number">4.</span> <span class="nav-text">泛型嵌套（Nested Generics）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Swift版本的可用性"><span class="nav-number">5.</span> <span class="nav-text">Swift版本的可用性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#逃逸闭包（Escaping-Closures）"><span class="nav-number">6.</span> <span class="nav-text">逃逸闭包（Escaping Closures）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Swift-Package-Manager-更新"><span class="nav-number">7.</span> <span class="nav-text">Swift Package Manager 更新</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#可编辑软件包（Editable-Packages）"><span class="nav-number">7.0.1.</span> <span class="nav-text">可编辑软件包（Editable Packages）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#版本固定（Version-Pinning）"><span class="nav-number">7.0.2.</span> <span class="nav-text">版本固定（Version Pinning）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#其他"><span class="nav-number">7.0.3.</span> <span class="nav-text">其他</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#其他改动"><span class="nav-number">8.</span> <span class="nav-text">其他改动</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#多重返回函数"><span class="nav-number">8.0.1.</span> <span class="nav-text">多重返回函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#自动链接失效（Disable-Auto-Linking）"><span class="nav-number">8.0.2.</span> <span class="nav-text">自动链接失效（Disable Auto-Linking）</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结语"><span class="nav-number">9.</span> <span class="nav-text">结语</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#著作权声明"><span class="nav-number">10.</span> <span class="nav-text">著作权声明</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        
<!--<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>-->

<div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">廖壬</span>

  
</div>

<!-- 在网站底部加上访问量-->
<!--<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_pv"></span>
</span>
</div>-->
<!--pv的方式，单个用户连续点击n篇文章，记录n次访问量-->
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



-->
<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共字</span>
</div>
        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  









  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>